package com.tuyrt.hi.ui.tab.bottom

import android.content.Context
import android.graphics.Color
import android.graphics.Typeface
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.annotation.ColorInt
import com.tuyrt.hi.ui.R
import com.tuyrt.hi.ui.tab.common.IHiTab

/**
 * author: Administrator on 2021/5/24.
 * desc:
 */
class HiTabBottom @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr), IHiTab<HiTabBottomInfo<*>> {

    private var tabInfo: HiTabBottomInfo<*>? = null
    private var tabIconView: TextView? = null
    private var tabNameView: TextView? = null
    private var tabImageView: ImageView? = null

    init {
        LayoutInflater.from(context).inflate(R.layout.hi_tab_bottom, this, true)
        tabIconView = findViewById(R.id.tv_icon)
        tabNameView = findViewById(R.id.tv_name)
        tabImageView = findViewById(R.id.iv_image)
    }

    fun getTabIconView() = tabIconView
    fun getTabNameView() = tabNameView
    fun getTabImageView() = tabImageView
    fun getHiTabInfo(): HiTabBottomInfo<*>? = tabInfo

    override fun setHiTabInfo(data: HiTabBottomInfo<*>) {
        tabInfo = data
        // 重新设置数据时，选中状态为 false，初始化为 true
        inflateInfo(false, true)
    }

    private fun inflateInfo(selected: Boolean, init: Boolean) {
        tabInfo?.let { tabInfo ->
            when (tabInfo.tabType) {
                HiTabBottomInfo.TabType.ICON -> {
                    if (init) {
                        tabImageView?.visibility = GONE
                        tabIconView?.let {
                            it.visibility = VISIBLE
                            val typeface = Typeface.createFromAsset(
                                context.assets,
                                tabInfo.iconFont
                            )
                            it.typeface = typeface
                        }
                        if (!tabInfo.name.isNullOrEmpty()) {
                            tabNameView?.text = tabInfo.name
                        }
                    }
                    if (selected) {
                        tabIconView?.let {
                            it.text =
                                if (TextUtils.isEmpty(tabInfo.selectedIconName)) tabInfo.defaultIconName else tabInfo.selectedIconName
                            it.setTextColor(getTextColor(tabInfo.tintColor))
                        }
                        tabNameView?.setTextColor(getTextColor(tabInfo.tintColor))
                    } else {
                        tabIconView?.let {
                            it.text = tabInfo.defaultIconName
                            it.setTextColor(getTextColor(tabInfo.defaultColor))
                        }
                        tabNameView?.setTextColor(getTextColor(tabInfo.defaultColor))
                    }
                }
                HiTabBottomInfo.TabType.BITMAP -> {
                    if (init) {
                        tabImageView?.visibility = VISIBLE
                        tabIconView?.visibility = GONE
                        if (!tabInfo.name.isNullOrEmpty()) {
                            tabNameView?.text = tabInfo.name
                        }
                    }
                    val bitmap = if (selected) tabInfo.selectedBitmap else tabInfo.defaultBitmap
                    tabImageView?.setImageBitmap(bitmap)
                }
            }
        } ?: throw IllegalArgumentException("tabInfo should not be null")
    }

    override fun onTabSelectedChange(
        index: Int,
        preInfo: HiTabBottomInfo<*>?,
        nextInfo: HiTabBottomInfo<*>
    ) {
        if (preInfo !== tabInfo && nextInfo !== tabInfo || preInfo === nextInfo) {
            return
        }

        if (preInfo === tabInfo) { // 选择 tabInfo 之外的其他 tabBottom
            inflateInfo(false, false)
        } else {
            inflateInfo(true, false)
        }
    }

    override fun resetHeight(height: Int) {
        val params = layoutParams
        params.height = height
        layoutParams = params
        getTabNameView()?.visibility = GONE
    }

    @ColorInt
    private fun getTextColor(color: Any): Int {
        return if (color is String) {
            Color.parseColor(color)
        } else {
            color as Int
        }
    }
}